function [log_likelihood]=ml_time_simple_ind_cdf(theta)
%%% Gives likelihood of observing an individual's 55 lottery choices and 48
%%% intertemporal choices under CRRA utility. Takes risk parameters as
%%% given (estimated in previous step based on lottery choices only) and
%%% now estimates time parameters based on the previous risk estimates and
%%% the observed intertemporal choices


 global  ind_n x1 x2 t1 t2 theta_xy lottery_n lottery_choice_ind time_choice_n time_choice_ind   ind  r_scale first_stage
    

    est_risk_pref=first_stage;
    rows=ind_n;
    tt=1;
    likelihood_multiplier=2;
    task_n=lottery_n+time_choice_n;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Lottery Part %%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% All Types  %%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%
    
     %%% Individual's true (or average) coefficient of risk aversion  
    theta_i=est_risk_pref(1);
    
    kappa_mean=first_stage(2);
    sig_th_mean=first_stage(3);
    
    %%% Individual's trembling hand parameter     
    kappa_l=0.5*normcdf(repmat(kappa_mean,1,lottery_n));    
    
    %%% individual variability of risk aversion 
    sig_th_l=normcdf(repmat(sig_th_mean,1,lottery_n));
    

    %%% difference between an individual's true (or average) coefficient of
    %%% risk aversion and the threshold level of indifference for lottery
    %%% task l
    theta_xy_comp=theta_xy;
    theta_diff=(bsxfun(@plus,theta_xy_comp,-theta_i));
    temp=zeros(size(theta_diff));
    for l=1:lottery_n
        temp(:,l)=theta_diff(:,l)./sig_th_l(:,l);
    end
    
    %%% Probability of choosing the riskier option 
    p_risky=normcdf(temp);
    p_safe=1-normcdf(temp);

    % kappa percent of the time an individual makes the wrong choice.
    choice_risky=zeros(size(p_risky));
    for l=1:lottery_n
        choice_risky(:,l)=p_risky(:,l).*(1-kappa_l(:,l))+p_safe(:,l).*kappa_l(:,l);
    end
    choice_safe=1-choice_risky;
    sim_l_contrib_lottery=(choice_risky.^(lottery_choice_ind(ind,:))).*(choice_safe.^(1-lottery_choice_ind(ind,:)));
   
        
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Time Part %%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% All Types  %%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%
            
    
     %%% parameter assignment for time analysis
  
    r_mean=theta(1);
    sig_r_mean=theta(2);
    theta(1:2)=[]; 
    
    r_i=r_scale*normcdf(r_mean);
    sig_r_i=normcdf(sig_r_mean);

        
    %%% Need to keep utility positive, so theta<1
    theta_limit=min(mean(theta_i), .99);
    theta_limit=max(theta_limit, -.3);            
    U_1=((x1.^(1-theta_limit))./(1-theta_limit));
    U_2=((x2.^(1-theta_limit))./(1-theta_limit));

     %%% indifference threshold given task parameters and the individual's
     %%% estimated coefficient of risk aversion
     r_xy_comp=(U_2./U_1).^(1./(t2-t1))-1;
    
  
    %%% Assumes lognormal errors on the delay aversion parameter, so log:
    %%% ln(r/sig_r)~N(0,1).  
    r_i2=r_i.^2;
    %%% variance of the error term (with a lognormal distrib) on the discount
    %%% rate
    var=(sig_r_i).^2;
    %%% Formulas for mean and sd of normal distrib of which the lognormal
    %%% distrib with mean=r_i and sd=sig_r is the exponent. Done to get pdf,
    %%% cdf of the lognormal distrib which in Matlab takes as parameters the
    %%% mean and sd of the corresponding normal distrib of which it is the log.
    % mean of standard normal distrib
    mu=log(r_i2./sqrt(var+r_i2));
    % sd of standard normal distrib. Adds the small number at the end
    % for purposes of numerical optimization
    sigma=sqrt(log(var./r_i2+1))+(1*10^-20);

    mu_full_time=repmat(mu,1,time_choice_n);
    sigma_full_time=repmat(sigma,1,time_choice_n);

    %%% Probability of choosing the later option i.e. probability that the
    %%% individual's discount rate is below the threshold for a
    %%% particular choice task, given the individual's coefficient of
    %%% risk aversion. Need to take the log here of the 
    %%% resulting difference divided by sig_r as it is the log of the
    %%% error which is normally distributed. 
    p_distant=logncdf(r_xy_comp,mu_full_time,sigma_full_time);
    p_near=1-p_distant;

    % kappa percent of the time and individual makes the wrong choice.
    choice_distant=zeros(size(p_distant));
    for i=1:time_choice_n
        choice_distant(:,i)=p_distant(:,i).*(1-kappa_l(:,tt))+p_near(:,i).*kappa_l(:,tt);
    end
    choice_near=1-choice_distant;
    sim_l_contrib_time=(choice_distant.^time_choice_ind(ind,:)).*(choice_near.^(1-time_choice_ind(ind,:)));   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End of Time Part %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        
        
        
   %%% full likelihood contribution vector
   sim_l_contrib_total=[sim_l_contrib_lottery sim_l_contrib_time];


    %%% for numerical optimization
    sim_l_contrib_total_plus=sim_l_contrib_total*likelihood_multiplier+1*10^(-20);

    %%% full likelihood contribution 
    sim_ind_draw_contrib=prod(sim_l_contrib_total_plus,2);
    
    %%% log likelihood
    log_likelihood=-sim_ind_draw_contrib;

       
end